package com.charm.watercress.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.charm.codes.mvp.factory.CreatePresenter
import com.charm.codes.mvp.view.AbstractFragment
import com.charm.watercress.R
import com.charm.watercress.adapter.FilmAdapter
import com.charm.watercress.constract.view.FilmView
import com.charm.watercress.constract.presenter.FilmPresenter
import com.charm.watercress.entity.FilmEntity
import android.support.v4.app.ActivityOptionsCompat
import kotlinx.android.synthetic.main.fragment_film.*

/**
 * Created by tools on 2018/1/11.
 */
@CreatePresenter(FilmPresenter::class)
@Route(path = "/watercress/film")
class FilmFragment : AbstractFragment<FilmView, FilmPresenter>(), FilmView {

    var page = 1
    private lateinit var adapter: FilmAdapter
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater!!.inflate(R.layout.fragment_film, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rv.layoutManager = LinearLayoutManager(context)
        initListener()
        initData()
    }

    private fun initData() {
        mvpPresenter.filmList(page)
        rv.adapter = adapter
    }

    private fun initListener() {
        adapter = FilmAdapter(null)
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT)
        refresh.setOnRefreshListener {
            page = 1
            adapter.data.clear()
            mvpPresenter.filmList(page)
        }

        adapter.setOnLoadMoreListener({
            page++
            mvpPresenter.filmList(page)
        }, rv)
//        adapter.setOnItemClickListener { adapter, view, position ->
//            val optionsCompat = ActivityOptionsCompat
//                    .makeSceneTransitionAnimation(
//                            activity,
//                            view.findViewById<ImageView>(R.id.image),
//                            "activityOption")
//            var entity = adapter.data[position]
//            ARouter.getInstance()
//                    .build("/watercress/film/detail")
//                    .withOptionsCompat(optionsCompat)
//                    .withObject("entity", entity)
//                    .navigation()
//        }

        adapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.image) {
                val optionsCompat = ActivityOptionsCompat
                        .makeSceneTransitionAnimation(
                                activity,
                                view,
                                "activityOption")
                var entity = adapter.data[position]
                ARouter.getInstance()
                        .build("/watercress/film/detail")
                        .withOptionsCompat(optionsCompat)
                        .withObject("entity", entity)
                        .navigation(activity)
            }

        }
    }
    override fun filmList(list: List<FilmEntity>) {
        adapter.addData(list)
        netComplete()
    }

    override fun netComplete() {
        if (refresh.isRefreshing) {
            refresh.finishRefresh()
        }
        if (adapter.isLoading) {
            adapter.loadMoreComplete()
        }
    }

}